package pshop.servlet.util;

import pshop.Bestellung;
import pshop.Warenkorb;
import pshop.servlet.ServletConstants;

import javax.servlet.http.HttpServletRequest;

public abstract class SessionUtils
{
   public static Warenkorb getSessionWarenkorb(HttpServletRequest req)
   {
      Warenkorb wk = (Warenkorb)req.getSession().getAttribute(ServletConstants.ATTR_WK);
      if (wk == null)
      {
         wk = new Warenkorb();
         req.getSession().setAttribute(ServletConstants.ATTR_WK, wk);
      }
      return wk;
   }

   public static Bestellung getSessionBestellung(HttpServletRequest req)
   {
      Bestellung best = (Bestellung)req.getSession().getAttribute(ServletConstants.ATTR_BEST);
      if (best == null)
      {
         best = new Bestellung();
         best.setWarenkorb(getSessionWarenkorb(req));
         req.getSession().setAttribute(ServletConstants.ATTR_BEST, best);
      }
      return best;
   }
}